package com.fenghongzhang.module_main.glide.nice_pic

import android.content.Context
import android.graphics.Bitmap


import android.util.TypedValue
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.fenghongzhang.lib_net.R
import com.fenghongzhang.lib_net.model.Image
import com.fenghongzhang.module_main.glide.glide.GlideApp



import jp.wasabeef.glide.transformations.RoundedCornersTransformation


/**
 *@author Wcj
 *@description
 *@date 2022/7/5 15:42
 */
class NinePicsAdapter(private val picItems: List<Image>, private val imageClick: (Int) -> Unit) :
    RecyclerView.Adapter<NinePicsAdapter.PicViewHolder>() {

    private lateinit var context: Context
    private val fgId by lazy {
        val typedValue = TypedValue()
        context.theme.resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true)
        typedValue.resourceId
    }


    override fun getItemViewType(position: Int): Int {
        if (picItems.any { it.imageWidth == 0 || it.imageHeight == 0 }) return NINE.value
        return if (picItems.isEmpty()) {
            NONE.value
        } else if (picItems.size == 1) {
            val image = picItems[0]
            if (image.imageWidth >= image.imageHeight) {
                LANDSCAPE.value
            } else {
                PORTRAIT.value
            }
        } else {
            NINE.value
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PicViewHolder {
        context = parent.context
        val imageView = ImageView(context).apply {
            scaleType = ImageView.ScaleType.CENTER_CROP
            setBackgroundResource(R.drawable.shape_bg_mask_round_4dp)
            foreground = context.getDrawable(R.drawable.shape_bg_normal_ripple_with_round_mask)
        }
        when (viewType) {
            NINE.value -> {
                return PicViewHolder(imageView, 110,110)
            }

            LANDSCAPE.value -> {
                return PicViewHolder(imageView, 230,170)
            }

            PORTRAIT.value -> {
                return PicViewHolder(imageView, 230, 310)
            }
        }
        return PicViewHolder(imageView, 110, 110)
    }

    override fun onBindViewHolder(holder: PicViewHolder, position: Int) {
        val isVideo = picItems[position].contentType.contains("video")

        val thumbnailUrl = picItems[position].run {
            if (isVideo){
                this.url
            } else {
                this.thumbnailUrl?.ifEmpty { this.url  }?:this.url
            }
        }
        holder.apply {

            val req = GlideApp.with(itemView.context)
                .asBitmap()
                //使用缩略图
                .load(thumbnailUrl)
                .listener(object : RequestListener<Bitmap> {
                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
                        itemView.setOnClickListener {
                           // ToastUtils.showShort(context, context.getString(R.string.string_image_error))
                        }
                        return false
                    }

                    override fun onResourceReady(
                        resource: Bitmap?,
                        model: Any?,
                        target: Target<Bitmap>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        itemView.setOnClickListener {
                            imageClick.invoke(holder.absoluteAdapterPosition)
                        }
                        return false
                    }
                })
            if (picItems.any { it.imageWidth == 0 || it.imageHeight == 0 }) {
                req.applyRoundPic(4, RoundedCornersTransformation.CornerType.ALL, 110, 110)
            } else {
                if (picItems.size > 1 || picItems.isEmpty()) {
                    req.applyRoundPic(4, RoundedCornersTransformation.CornerType.ALL, 110, 110)
                } else {
                    if (picItems[0].imageWidth >= picItems[0].imageHeight) {
                        req.applyRoundPic(4, RoundedCornersTransformation.CornerType.ALL, 230, 170)
                    } else {
                        req.applyRoundPic(4, RoundedCornersTransformation.CornerType.ALL, 230, 310)
                    }
                }
            }
            req.into(itemView as ImageView)
        }


//        holder.apply {
//            holder.itemView.setTag(R.id.imageTag, position)
//            GlideApp.with(itemView.context)
//                .asBitmap()
//                .load(if (picItems[position].isNullOrEmpty()) "" else AliOssUrl(picItems[position]))
//                .listener(object : RequestListener<Bitmap> {
//                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
//                        itemView.setOnClickListener {
//                            ToastUtils.showShort(context, context.getString(R.string.string_image_error))
//                        }
//                        return false
//                    }
//
//                    override fun onResourceReady(
//                        resource: Bitmap?,
//                        model: Any?,
//                        target: Target<Bitmap>?,
//                        dataSource: DataSource?,
//                        isFirstResource: Boolean
//                    ): Boolean {
//
//                        return false
//                    }
//                }).into(object : CustomTarget<Bitmap>() {
//                    override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
//                        val pos = holder.itemView.getTag(R.id.imageTag) as Int
//                        if (pos < 0 || picItems.size <= pos) {
//                            return
//                        }
//                        //GlideApp.with(holder.itemView).clear(holder.itemView)
//                        val iv = itemView as ImageView
//                        val req = GlideApp.with(iv).asBitmap()
//                            .load(if (picItems[pos].isEmpty()) "" else AliOssUrl(picItems[pos]))
//                            .placeholder((iv).drawable)
//                        val width = resource.width
//                        val height = resource.height
//                        val l = iv.layoutParams
//                        if (picItems.size == 1) {
//                            //设置单张imageview大小
//                            if (width >= height) {
//                                l.width = 230.dp
//                                l.height = 170.dp
//                            } else {
//                                l.width = 230.dp
//                                l.height = 310.dp
//                            }
//                            iv.layoutParams = l
//                            if (resource.width >= resource.height) {
//                                req.applyRoundPic(4.dp, RoundedCornersTransformation.CornerType.ALL, 230.dp, 170.dp)
//                            } else {
//                                req.applyRoundPic(4.dp, RoundedCornersTransformation.CornerType.ALL, 230.dp, 310.dp)
//                            }
//                            req.into(iv)
//                        } else {
//                            //九宫格
//                            l.width = 110.dp
//                            l.height = 110.dp
//                            iv.layoutParams = l
//                            req.applyRoundPic(4.dp, RoundedCornersTransformation.CornerType.ALL, 110.dp, 110.dp)
//                            req.into(iv)
//                        }
//                        iv.setOnClickListener {
//                            imageClick.invoke(pos)
//                        }
//
//
//                    }
//
//                    override fun onLoadCleared(placeholder: Drawable?) {
//
//                    }
//                })
//
//        }
    }

    override fun getItemCount(): Int = picItems.size


    inner class PicViewHolder(itemView: ImageView, width: Int, height: Int) : RecyclerView.ViewHolder(itemView) {

        init {
            itemView.layoutParams = ViewGroup.LayoutParams(width, height)
        }
    }
}

sealed class ViewType(val value: Int)
object NONE : ViewType(1)
object NINE : ViewType(2)
object PORTRAIT : ViewType(3)
object LANDSCAPE : ViewType(4)
object VIDEO : ViewType(5)
